package cn.liwq.demo.serializer;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializerBase;

import java.math.BigDecimal;

/**
 * BigDecimal转字符串，序列化
 *
 * @author lweiqiang
 * @date 2023/2/22
 */
public class BigDecimalToStringSerializer extends ToStringSerializerBase {
  /** Singleton instance to use. */
  public static final BigDecimalToStringSerializer INSTANCE = new BigDecimalToStringSerializer();

  public BigDecimalToStringSerializer() {
    super(Object.class);
  }

  /**
   * Sometimes it may actually make sense to retain actual handled type.
   *
   * @since 2.5
   */
  public BigDecimalToStringSerializer(Class<?> handledType) {
    super(handledType);
  }

  @Override
  public final String valueToString(Object value) {
    if (value instanceof BigDecimal) {
      return ((BigDecimal) value).toPlainString();
    }
    return value.toString();
  }
}
